package com.le.tester.imooc.masterJUC.threadpool;

import com.le.tester.utils.DateTimeUtilFor8;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 描述：关闭线程池
 */
public class ShutDownTester {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 100; i++) {
            executorService.execute(new shutDownTask());
        }
        Thread.sleep(1000);
        executorService.shutdownNow();
        //System.out.println(runnables);
        //进行了shutDown()之后，就不能在添加任务了
        //executorService.shutdown();
        //等待一段时间后，看线程池中的线程是不是已经把任务全部执行完毕了
        //因为没执行shutDown(),所以会一直是false
        //boolean b = executorService.awaitTermination(10L, TimeUnit.SECONDS);
        //System.out.println("awaitTermination:" + b);
//        System.out.println("isShutDown before:" + executorService.isShutdown());
//        //放出执行结束的信息，不再接收新的任务，并且等全部任务执行完之后，才真正的停止程序
//        executorService.shutdown();
//        System.out.println("isShutDown after:" + executorService.isShutdown());
//        Thread.sleep(10000);
//        //isTerminated整个程序是不是已经全部执行完毕
//        System.out.println("isTerminated:" + executorService.isTerminated());
//        //停止了，不能再运行其他任务了,当时还是得等待前面的任务执行完毕
//        //executorService.execute(new shutDownTask());
    }

}

class shutDownTask implements Runnable {
    @Override
    public void run() {
        try {
            Thread.sleep(500);
            System.out.println("当前时间：" + DateTimeUtilFor8.time() + " 当前线程：" + Thread.currentThread().getName());
        } catch (InterruptedException e) {
            //shutDown的使用是用了interrupt的中断信号
            System.out.println("被中断了：" + Thread.currentThread().getName());
        }
    }
}
